<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/component/pear/css/pear.css" />
    <script src="/static/component/layui/layui.js"></script>
    <script src="/static/component/pear/pear.js"></script>
    <!--加载插件-->
    <script src="/static/component/layui_exts/excel.js"></script>
</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
                        <div class="layui-form-item layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-col-sm5">
                                <input type="text" name="uid" placeholder="请输入用户ID" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline">
                            <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                                <i class="layui-icon layui-icon-search"></i>
                                查询
                            </button>
                            <button type="reset" class="pear-btn pear-btn-md">
                                <i class="layui-icon layui-icon-refresh"></i>
                                重置
                            </button>
                        </div>
                    </div>
				</form>
			</div>
		</div>
        <div>			
			<div class="layui-row layui-col-space10">
				<div class="layui-col-xs6 layui-col-md3">
					<div class="layui-card top-panel">
						<div class="layui-card-header">今日参与人数</div>
						<div class="layui-card-body">
							<div class="layui-row layui-col-space5">
								<div class="layui-col-xs8 layui-col-md8 top-panel-number" style="color: #28333E;" id="value1">
									{$shujuyi}
								</div>
								<div class="layui-col-xs4 layui-col-md4 top-panel-tips">
									<i class="layui-icon layui-icon-fire"></i>  
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="layui-col-xs6 layui-col-md3">
					<div class="layui-card top-panel">
						<div class="layui-card-header">昨日参与人数</div>
						<div class="layui-card-body">
							<div class="layui-row layui-col-space5">
								<div class="layui-col-xs8 layui-col-md8 top-panel-number" style="color: #28333E;" id="value2">
									{$shujuer}
								</div>
								<div class="layui-col-xs4 layui-col-md4 top-panel-tips">
									<i class="layui-icon layui-icon-transfer"></i>  
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="layui-col-xs6 layui-col-md3">
					<div class="layui-card top-panel">
						<div class="layui-card-header">七日参与人数</div>
						<div class="layui-card-body">
							<div class="layui-row layui-col-space5">
								<div class="layui-col-xs8 layui-col-md8 top-panel-number" style="color: #28333E;" id="value3">
									{$shujusan}
								</div>
								<div class="layui-col-xs4 layui-col-md4  top-panel-tips">
									<i class="layui-icon layui-icon-praise"></i>  
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="layui-col-xs6 layui-col-md3">
					<div class="layui-card top-panel">
						<div class="layui-card-header">总参与人数</div>
						<div class="layui-card-body">
							<div class="layui-row layui-col-space5">
								<div class="layui-col-xs8 layui-col-md8 top-panel-number" style="color: #28333E;" id="value4">
									{$shujusi}
								</div>
								<div class="layui-col-xs4 layui-col-md4 top-panel-tips">
									<i class="layui-icon layui-icon-username"></i>  
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="dataTable" lay-filter="dataTable"></table>
			</div>
		</div>
		<script type="text/html" id="toolbar">
            <button class="pear-btn pear-btn-md" style="float: right;" lay-event="daochu">
                <i class="layui-icon"></i>
                导出Excel
            </button>
		</script>
		<script type="text/html" id="options">
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" style="font-size: 6px !important;width: 55px;padding-right: 0px;padding-left: 0px;">删除</button>
		</script>
        <script type="text/html" id="imgtmp">
		    <img src="{{d.headimgurl}}" alt="" width="30px;" height="30px;" style="border-radius: 50%;">
		</script>
        <script>
        layui.use(['table', 'form', 'jquery', 'excel','common','laydate'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let common = layui.common;
            let laydate = layui.laydate;
            let excel = layui.excel;
            let MODULE_PATH = "{$Request.root}/admin.zbuserinfo/";

            let cols1 = [
                [{
                    field: "uid",
                    title: "用户ID",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "openid",
                    title: "openId",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "nickname",
                    title: "昵称",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "headimgurl",
                    title: "头像",
                    // sort:true,
                    // edit:true,
                    unresize: "true",
                    width:'10%',
                    align: "center",
                    templet:"#imgtmp"
                }, {
                    field: "laiyuan",
                    title: "来源",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "ip",
                    title: "IP",
                    unresize: "true",
                    align: "center"
                }, {
                    field: "datetime",
                    title: "参加时间",
                    unresize: "true",
                    align: "center"
                }, {$qitaxiang} {
                    title: '操作',
                    toolbar: '#options',
                    unresize: true,
                    align: 'center',
                    fixed: 'right',
                }]
            ]
            
            

            table.on('edit(dataTable)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                // console.log(obj.value); //得到修改后的值
                // console.log(obj.field); //当前编辑的字段名
                // console.log(obj.data.id); //所在行的所有相关数据  
                $.ajax({
                    url:'{$Request.root}/admin.zccandidate/gaipiao',
                    data:{id:obj.data.id,numpiao:obj.value},
                    dataType: 'json',
                    type: 'POST',
                    success: function(res) {
                        // console.log(res);
                        window.refresh();
                    }
                })
            });

            
                table.render({
                    elem: '#dataTable',
                    url: MODULE_PATH + 'index/id/'+{$biaoid},
                    page: true,
                    cols: cols1,
                    cellMinWidth: 100,
                    skin: 'line',
                    toolbar: '#toolbar',
                    defaultToolbar: [{
                        title: '刷新',
                        layEvent: 'refresh',
                        icon: 'layui-icon-refresh',
                    }, 'filter', 'print']
                });





                table.on('toolbar(dataTable)', function(obj) {
                    if (obj.event === 'add') {
                        window.add();
                    } else if (obj.event === 'refresh') {
                        window.refresh();
                    } else if (obj.event === 'batchRemove') {
                        window.batchRemove(obj);
                    } else if (obj.event === 'recycle') {
                        window.recycle(obj);
                    } else if (obj.event === 'qingkongshuju') {
                        window.qingkongshuju(obj);
                    } else if (obj.event === 'daochu') {
                        $.ajax({
                            url: '{$Request.root}/admin.zbuserinfo/getdata',
                            data:{biaoid:{$biaoid}},
                            dataType: 'json',
                            success: function(res) {
                                // 假如返回的 res.data 是需要导出的列表数据
                                // console.log(res.data);// [{name: 'wang', age: 18}, {name: 'layui', age: 3}]
                                // console.log(res.datatitle);
                                // LAY_EXCEL.exportExcel(res.data, res.datatitle.title+'得票统计.xlsx', 'xlsx');

                                // 1. 数组头部新增表头
                                res.data.unshift({uid: '用户ID',openid: 'openId', nickname: '昵称', headimgurl: '头像', laiyuan: '来源', ip: 'IP', datetime: '参加时间'});
                                // 2. 如果需要调整顺序，请执行梳理函数
                                var data = excel.filterExportData(res.data, [
                                    'uid',
                                    'openid',
                                    'nickname',
                                    'headimgurl',
                                    'laiyuan',
                                    'ip',
                                    'datetime',
                                ]);
                                // 3. 执行导出函数，系统会弹出弹框
                                excel.exportExcel({
                                    sheet1: data
                                }, res.datatitle.title+'得票统计.xlsx', 'xlsx');
                            }
                        });
                    }
                });
            
            

            table.on('tool(dataTable)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                } else if (obj.event === 'depiaotongji') {
                    window.depiaotongji(obj);
                }
            });

            

            form.on('submit(query)', function(data) {
                // console.log(data.field.group);
                $("[id='daochufenzu']").val(data.field.group);
                table.reload('dataTable', {
                    where: data.field,
                    page:{curr: 1}
                })
                
                return false;
            });
            
            //弹出窗设置 自己设置弹出百分比
            function screen() {
                if (typeof width !== 'number' || width === 0) {
                width = $(window).width() * 0.8;
                }
                if (typeof height !== 'number' || height === 0) {
                height = $(window).height() - 20;
                }
                return [width + 'px', height + 'px'];
            }

            window.add = function() {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '新增候选',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'add/biaoid/'+{$biaoid},
                });
            }

            window.edit = function(obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '修改候选',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'edit/id/'+obj.data['id']
                });
            }

            window.depiaotongji = function(obj) {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '得票统计',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'depiaotongji/id/'+obj.data['id']
                });
            }

            window.recycle = function() {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '回收站',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'recycle',
                    cancel: function () {
                        table.reload('dataTable');
                    }
                });
            }

            window.qingkongshuju = function() {
                layer.confirm('确定要清空吗', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    $.ajax({
                        url:'{$Request.root}/admin.zccandidate/qingkongshuju',
                        data:{id:{$biaoid}},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            // console.log(res);
                            layer.close(index);
                            window.refresh();
                        }
                    })
                });
            }


            window.remove = function(obj) {
                layer.confirm('确定要删除该候选', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'remove',
                        data:{id:obj.data['id']},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                    window.refresh();
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要删除这些候选', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'batchRemove',
                        data:{ids:ids,biaoid:{$biaoid}},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function(param) {
                table.reload('dataTable');
            }
        })
    </script>
	</body>
</html>
